import os
import numpy as np
import math


def write_axis(axis_path, point, vector):
    with open(axis_path, 'w') as f:
        f.write(str(point) + '\n')
        f.write(str(vector) + '\n')


def read_axis(axis_path):
    my_point = np.asarray([0., 0., 0.])
    my_vector = np.asarray([0., 0., 0.])
    with open(axis_path) as f:
        text = f.readlines()
        point_txt = text[0].replace('[', '').replace(']', '').split()
        my_point[0] = float(point_txt[0])
        my_point[1] = float(point_txt[1])
        my_point[2] = float(point_txt[2])
        vector_txt = text[1].replace('[', '').replace(']', '').split()
        my_vector[0] = float(vector_txt[0])
        my_vector[1] = float(vector_txt[1])
        my_vector[2] = float(vector_txt[2])
    return my_point, my_vector


def write_metrics(output_path, my_point, my_vector, manual_point, manual_vector, dist, spacing, a0, b0, a1, b1):
    with open(output_path, 'w') as f:
        f.write('calculated axis: \n')
        f.write('    point: ' + str(my_point) + '\n')
        f.write('    vector: ' + str(my_vector) + '\n')
        f.write('\n')
        f.write('manually labelled axis: \n')
        f.write('    point: ' + str(manual_point) + '\n')
        f.write('    vector: ' + str(manual_vector) + '\n')
        f.write('\n')
        f.write('distance(pixel): ' + str(dist) + '\n')
        f.write('spacing: ' + str(spacing) + '\n')
        f.write('distance(mm): ' + str(dist * spacing[0]) + '\n')
        f.write('angle of predicted: ' + str((np.rad2deg(a0), np.rad2deg(b0))) + '\n')
        f.write('angle of manual: ' + str((np.rad2deg(a1), np.rad2deg(b1))) + '\n')
        f.write('relative angle(degree): ' + str(np.rad2deg(math.acos(manual_vector.dot(my_vector)))) + '\n')


def read_metrics(metrics_path):
    """

    :param metrics_path:
    :return: d, (p_a, p_b), (m_a, m_b), theta
    """
    distance_index = 10
    predicted_angle_index = 11
    manual_angle_index = 12
    theta_index = 13
    with open(metrics_path) as f:
        lines = f.readlines()
        distance = float(lines[distance_index].split(':')[-1])
        p_a, p_b = map(float, lines[predicted_angle_index].split('(')[-1].replace(')', '').split(','))
        m_a, m_b = map(float, lines[manual_angle_index].split('(')[-1].replace(')', '').split(','))
        theta = float(lines[theta_index].split(':')[-1])
    return distance, (p_a, p_b), (m_a, m_b), theta


def write_statistics(path, dist_list, alpha_diff_list, beta_diff_list, theta_list):
    with open(path, 'w') as f:
        f.write('distances: ' + str(dist_list) + '\n')
        f.write('distance mean: ' + str(sum(dist_list) / len(dist_list)) + '\n')
        f.write('alpha_diff list: ' + str(alpha_diff_list) + '\n')
        f.write('beta_diff list: ' + str(beta_diff_list) + '\n')
        f.write('theta list: ' + str(theta_list) + '\n')
        f.write('theta mean: ' + str(sum(theta_list) / len(theta_list)) + '\n')


def write_tempinfo(path, center, size):
    with open(path, 'w') as f:
        f.write(str(center) + '\n')
        f.write(str(size) + '\n')


def read_tempinfo(path):
    center_heart = np.zeros(3)
    ori_size = np.zeros(3)
    with open(path) as f:
        text = f.readlines()
        center_line = text[0].replace('(', '').replace(')', '').split(',')
        center_heart[0] = float(center_line[0])
        center_heart[1] = float(center_line[1])
        center_heart[2] = float(center_line[2])

        size_line = text[1].replace('(', '').replace(')', '').split(',')
        ori_size[0] = int(size_line[0])
        ori_size[1] = int(size_line[1])
        ori_size[2] = int(size_line[2])

    center_heart = center_heart.astype(np.float64)
    return center_heart, ori_size


def write_template_info(path, size, spacing, origin, direction):
    with open(path, 'w') as f:
        f.write(str(size) + '\n')
        f.write(str(spacing) + '\n')
        f.write(str(origin) + '\n')
        f.write(str(direction) + '\n')


def read_template_info(path):
    #TODO: size, spacing, origin, direction
    with open(path, 'r') as f:
        text = f.readlines()
        size = tuple(map(int, text[0].replace('(', '').replace(')', '').split(',')))
        spacing = tuple(map(float, text[1].replace('(', '').replace(')', '').split(',')))
        origin = tuple(map(float, text[2].replace('(', '').replace(')', '').split(',')))
        direction = tuple(map(float, text[3].replace('(', '').replace(')', '').split(',')))
        
    return size, spacing, origin, direction